home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / turbotut.arc / MANUAL.EXE / lha / CHAP13.TXT < prev    next >
Text File  |  1988-11-27  |  12KB  |  256 lines

  1.                   CHAPTER 13 - Complete sample programs
  2.  
  3.  
  4.             Prior  to this point,  this tutorial has given you  many
  5.         example  programs  illustrating a point of  some  kind,  but
  6.         these  have  all been "nonsense" programs as  far  as  being
  7.         useful.  It would be a disservice to you to simply quit with
  8.         only  program  fragments to study so the following  programs
  9.         are  offered to you as examples of good  Pascal  programming
  10.         practice.   They  are  useful programs,  but they are  still
  11.         short enough to easily grasp their meaning.  We will discuss
  12.         them one at a time.
  13.  
  14.                        AMORTIZATION TABLE GENERATOR
  15.  
  16.             This  is  not one program,  but five.   Each one  is  an
  17.         improvement on the previous one,  and the series is intended
  18.         to give you an idea of program development.
  19.  
  20.         AMORT1  - This  is  the  bare outline  of  the  amortization
  21.                 program.  Although  it is an operating  program,  it
  22.                 doesn't  do  very  much.   After  some  thought  and
  23.                 planning,  the main program was written to allow for
  24.                 an  initialization,  then an annual repeating  loop.
  25.                 The  annual loop would require a header,  a  monthly
  26.                 calculation,  and  an  annual  balance.  Finally,  a
  27.                 procedure  was outlined for each of these  functions
  28.                 with  a  minimum of calculations in each  procedure.
  29.                 This program can be compiled and run to see that  it
  30.                 does  do something for each month and for each year.
  31.                 It has a major problem because it does not stop when
  32.                 the loan is payed off but keeps going to the end  of
  33.                 that year. The primary structure is complete.
  34.  
  35.         AMORT2  - This  is an improvement over AMORT1.  The  monthly
  36.                 calculations  are  correct but the final payment  is
  37.                 still  incorrectly  done.  Notice that for  ease  of
  38.                 testing,  the  loan variables are simply defined  as
  39.                 constants in the initialize procedure.  To make  the
  40.                 procedures  easier to find,  comments with asterisks
  41.                 were added.  This program is nearly usable.  Compile
  42.                 and run it.
  43.  
  44.         AMORT3 - Now we calculate the final payment correctly and we
  45.                 have  a correct annual header with column  headings.
  46.                 We have introduced a new variable to be used for  an
  47.                 annual  interest accumulation.  This is neat to have
  48.                 at  income  tax  time.  This  program  can  also  be
  49.                 compiled and run.
  50.  
  51.         AMORT4  - This program does nearly everything we would  like
  52.                 it to do. All of the information needed to build the
  53.                 table for any loan is now read in from the keyboard,
  54.                 greatly   adding  to  the  flexibility.   After  the
  55.  
  56.  
  57.                                  Page 68
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                   CHAPTER 13 - Complete sample programs
  68.  
  69.  
  70.                 information  is available,  the monthly  payment  is
  71.                 calculated    in    the   newly   added    procedure
  72.                 "calculate_payment".  The  annual header has  a  new
  73.                 line  added to include the original loan amount  and
  74.                 the  interest rate in the information.  Compile  and
  75.                 run this program to see its operation.
  76.  
  77.         AMORT5 - The  only additional feature in this program is the
  78.                 addition of a printout of the results. Examining the
  79.                 program,  you  will notice that many of  the  output
  80.                 statements  are  duplicated with the "lst"  included
  81.                 for  the  device selection.  Compile  and  run  this
  82.                 program,  but be sure to turn your printer on to get
  83.                 a printout of the amortization table you ask for.
  84.  
  85.                              TOP DOWN PROGRAMMING
  86.  
  87.             The  preceding  example  is an  example  of  a  top-down
  88.         approach to programming.   This is where the overall task is
  89.         outlined,  and  the  details are added in  whatever  fashion
  90.         makes  sense to the designer.   The opposite is a  bottom-up
  91.         programming  effort,  in  which the heart of the problem  is
  92.         defined  and the rest of the program is built up around  it.
  93.         In this case, the monthly payment schedule would probably be
  94.         a  starting  point and the remainder of the  program  slowly
  95.         built  up around it.   Use whichever method works  best  for
  96.         you.
  97.  
  98.             The  final program AMORT5 is by no means a program which
  99.         can  never  be  improved upon.   Many  improvements  can  be
  100.         thought  of.   These  will be exercises for you  if  you  so
  101.         desire.
  102.  
  103.         1.  In the data input section, ask if a printout is desired,
  104.             and  only print if it was requested.  This would involve
  105.             defining  a new variable and IF  statements  controlling
  106.             all write statements with "lst" as a device selector.
  107.  
  108.         2.  Format the printout with a formfeed every three years to
  109.             cause  a neater printout.  The program presently  prints
  110.             data right across the paper folds  with no regard to the
  111.             top of page.
  112.  
  113.         3.  Modify  the  program  to include  semimonthly  payments.
  114.             Payments  twice a month are becoming popular,  but  this
  115.             program cannot handle them.
  116.  
  117.         4.  Instead of listing the months as numbers,  put in a CASE
  118.             statement to cause the months to be printed out as three
  119.             letter names.  Or include the day of the month when  the
  120.             payment is due also.
  121.  
  122.  
  123.                                  Page 69
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                   CHAPTER 13 - Complete sample programs
  134.  
  135.  
  136.  
  137.         5.  Any  other modification you can think up.  The more  you
  138.             modify this and other programs,  the more experience and
  139.             confidence you will gain.
  140.  
  141.                     LIST, to list your Pascal programs
  142.  
  143.             LIST  is a very useful program that you can use to  list
  144.         your  Pascal  programs  on  the printer.   It  can  only  be
  145.         compiled with TURBO Pascal because it uses TURBO extensions.
  146.         The two extensions it uses are the STRING type variable  and
  147.         the  ABSOLUTE type variable.   The ABSOLUTE variable in line
  148.         13  and  the  coding in the  "initialize"  procedure  is  an
  149.         example  of how you can read in the parameters given on  the
  150.         command line.  For example, to use this program to print out
  151.         the  last program,  you would enter the following at the DOS
  152.         prompt   LIST  AMORT5.PAS.    This  program  reads  in   the
  153.         AMORT5.PAS  from the command line and uses it to define  the
  154.         input  file.   It  should be pointed out that  this  program
  155.         cannot  be run from a "compiled in memory" compilation  with
  156.         the  TURBO Pascal compiler.   It must be compiled to  a  COM
  157.         file, and you must quit TURBO Pascal in order to run it from
  158.         the DOS command level.
  159.  
  160.              The parameter, AMORT5.PAS, is stored at computer memory
  161.         location   80(hexadecimal)  referred  to  the  present  code
  162.         segment.   If you didn't understand that,  don't worry,  you
  163.         can still find the input parameter in any program using  the
  164.         method given in the initialize procedure.
  165.  
  166.             If  you do not have TURBO Pascal,  but you are using MS-
  167.         DOS or PC-DOS,  you can still use this program because it is
  168.         on  your disk already compiled as LIST.COM,  and can be  run
  169.         like any other .COM or .EXE program.
  170.  
  171.                       LIST2, another listing program
  172.  
  173.             LIST2 is the same as LIST, except that it does not parse
  174.         the command line for the filename.  Instead, it requests the
  175.         desired  filename to output,  and you input it as user  data
  176.         after  the  program  begins executing.   It  is  written  in
  177.         "generic" Pascal and should execute and run with any  Pascal
  178.         compiler.
  179.  
  180.                   TIMEDATE, to get today's time and date
  181.  
  182.             This  is  a very useful program for those of  you  using
  183.         TURBO Pascal.  It interrogates the inner workings of DOS and
  184.         gets the present time and date for you, provided you entered
  185.         them  correctly  when  you turned  your  computer  on.   The
  186.         procedure  "time_and_date"  can  be included in  any  Pascal
  187.  
  188.  
  189.                                  Page 70
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                   CHAPTER 13 - Complete sample programs
  200.  
  201.  
  202.         program  you  write to give you the time and date  for  your
  203.         listings.   As an exercise in programming,  add the time and
  204.         date to the program LIST to improve on its usefulness.
  205.  
  206.                         AREAS, an example of menus
  207.  
  208.             This program is not very useful,  but it illustrates one
  209.         way to handle menus in a Pascal program.   You can study the
  210.         structure  and  imagine  many ways a menu  can  be  used  to
  211.         improve the usefulness of your own programs.
  212.  
  213.                      OT, The OAKTREE directory program
  214.  
  215.              This  program should be very useful to you,  especially
  216.         if you have a hard disk.   It will list the entire  contents
  217.         of  your  hard disk (or floppy) in a very easy to  read  and
  218.         easy to use form.   The program is documented in OT.DOC, and
  219.         is  precompiled for you in OT.COM in case you are not  using
  220.         TURBO  Pascal.   It uses many of the TURBO Pascal extensions
  221.         and will probably not compile with any other Pascal compiler
  222.         without extensive modifications.
  223.  
  224.              You  will  find  the program to be a  good  example  of
  225.         linked  lists  because it includes a sort  routine  using  a
  226.         dynamically  allocated  B-TREE and another  sorting  routine
  227.         that  uses  a  dynamically  allocated  linked  list  with  a
  228.         "bubble_sort".   These  methods  are completely  defined  in
  229.         Niklaus  Wirth's  book,  "Algorithms  +  Data  Structures  =
  230.         Programs",  a  highly recommended book if you are interested
  231.         in advanced programming techniques.
  232.  
  233.              It might also be pointed out that OT.PAS also makes use
  234.         of   recursive  methods  for  both  sorting   and   handling
  235.         subdirectories.   It  is  definitely an example of  advanced
  236.         programming methods, and it would be a good vehicle for your
  237.         personal study.
  238.  
  239.                      Most Important - Your own programs
  240.  
  241.             Having completed this tutorial on Pascal,  you are  well
  242.         on your way to becoming a proficient Pascal programmer.  The
  243.         best  way  you  can improve your skills now is  to  actually
  244.         write Pascal programs.   Another way to aid in your building
  245.         of  skill and confidence is to study other Pascal  programs.
  246.         Many   programming  examples  can  be  found  in   computing
  247.         magazines  and books.   One of the best books  available  is
  248.         "Programming  in  Pascal" by Peter Grogono,  and another  is
  249.         "Oh! Pascal!" by Doug Cooper and Michael Clancy.
  250.  
  251.             Happy programming.
  252.  
  253.  
  254.  
  255.                                  Page 71
  256.